package cn.wolfcode.rbac.web.controller;

import cn.wolfcode.rbac.query.CustomerReportQuery;
import cn.wolfcode.rbac.service.ICustomerReportService;
import cn.wolfcode.rbac.util.MessageUtil;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/customerReport")
public class CustomerReportController {
    @Autowired
    private ICustomerReportService customerReportService;
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo")CustomerReportQuery qo){
        model.addAttribute("result",customerReportService.selectCustomerReport(qo));
        return "customerReport/list";
    }

    //Echarts   柱状图
    @RequestMapping("/listByBar")
    public String listByBar(Model model,@ModelAttribute("qo")CustomerReportQuery qo){
        PageInfo pageInfo = customerReportService.selectCustomerReport(qo);
        //获取结果集，因为存进去的就是map
        List<Map> list = pageInfo.getList();
        //提供一个集合存x轴数据
        ArrayList xList = new ArrayList();
        //提供一个集合存y轴数据
        ArrayList yList = new ArrayList();
        //需要把数据转换为Echarts需要结构的数据，x和y轴的数据要分开
        for(Map map : list){
            //对应sql语句中的两个值
            xList.add(map.get("groupType"));
            yList.add(map.get("number"));
        }
        //共享数据到页面   json转一下，否则页面收不到
        model.addAttribute("xList", JSON.toJSONString(xList));
        model.addAttribute("yList",JSON.toJSONString(yList));
        //最后模态框用这个
        model.addAttribute("groupTypeName", MessageUtil.changMsg(qo));
        return "customerReport/listByBar";
    }
}
